package zakladki.AS3 {
import mx.controls.CheckBox;
import mx.controls.ComboBox;
import mx.core.FlexGlobals;


public class FoodChangeListener {

    private var mainApp:Fossilminator = FlexGlobals.topLevelApplication as Fossilminator;

    private var foodAmountCombo:ComboBox;
    private var foodOriginCombo:ComboBox;
    private var meatConsumptionCombo:ComboBox;
    private var frozenFoodCombo:ComboBox;
    private var fridgeCombo:ComboBox;
    private var cookerCombo:ComboBox;

    private var separateFridgeCheckBox:CheckBox;
    private var washingMachineCheckBox:CheckBox;

    public function FoodChangeListener(foodAmountCombo:ComboBox, foodOriginCombo:ComboBox, meatConsumptionCombo:ComboBox, frozenFoodCombo:ComboBox, fridgeCombo:ComboBox, separateFridgeChechBox:CheckBox, washingMachineCheckBox:CheckBox, cookerCombo:ComboBox) {
        this.foodAmountCombo = foodAmountCombo;
        this.foodOriginCombo = foodOriginCombo;
        this.meatConsumptionCombo = meatConsumptionCombo;
        this.frozenFoodCombo = frozenFoodCombo;
        this.fridgeCombo = fridgeCombo;
        this.separateFridgeCheckBox = separateFridgeChechBox;
        this.washingMachineCheckBox = washingMachineCheckBox;
        this.cookerCombo = cookerCombo;
    }

    private function createFoodSelectedData():FoodSelectedData{
        return new FoodSelectedData(getComboData(foodAmountCombo),getComboData(foodOriginCombo),getComboData(meatConsumptionCombo),getComboData(frozenFoodCombo),getComboData(fridgeCombo),
                                    separateFridgeCheckBox.selected,washingMachineCheckBox.selected,getComboData(cookerCombo));

    }

    private function getComboData(combo:ComboBox):int {
        return combo.selectedItem.data;
    }

    public function foodAmountChanged(calc:Boolean):void {
        var foodData:FoodSelectedData = createFoodSelectedData();

        mainApp.widok_sceny.Kuch.iloscJedzenia(foodData.foodAmount);
        if (calc) {
            updatePerson(foodData);
            mainApp.calculate();
        }
    }

    public function foodOriginChanged(calc:Boolean):void {
        var foodData:FoodSelectedData = createFoodSelectedData();
        mainApp.widok_sceny.Kuch.pochodzenieZywnosci(foodData.foodOrigin);
        if (calc) {
            updatePerson(foodData);
            mainApp.calculate();
        }
    }

    public function meatConsumptionChanged(calc:Boolean):void {
        var foodData:FoodSelectedData = createFoodSelectedData();
        mainApp.widok_sceny.Kuch.iloscMiesa(foodData.meatConsumption);
        if (calc) {
            updatePerson(foodData);
            mainApp.calculate();
        }
    }

    public function frozenFoodChanged(calc:Boolean):void {
        var foodData:FoodSelectedData = createFoodSelectedData();
        mainApp.widok_sceny.Kuch.ZywnoscMrozona(foodData.frozenFood);
        if (calc) {
            updatePerson(foodData);
            mainApp.calculate();
        }
    }

    public function fridgeTypeChanged(calc:Boolean):void {
        var foodData:FoodSelectedData = createFoodSelectedData();
        mainApp.widok_sceny.Kuch.Lodowka(foodData.fridgeType, foodData.separateFridge);
        if (calc) {
            updatePerson(foodData);
            mainApp.calculate();
        }
    }

    public function cookerTypeChanged(calc:Boolean):void {
        var foodData:FoodSelectedData = createFoodSelectedData();
        //mx.core.mainApp.widok_sceny.Kuch.
        if (calc) {
            updatePerson(foodData);
            mainApp.calculate();
        }
    }

    public function separateFridgeChanged(calc:Boolean):void {
        var foodData:FoodSelectedData = createFoodSelectedData();
        mainApp.widok_sceny.Kuch.Lodowka(foodData.fridgeType, foodData.separateFridge);
        if (calc) {
            updatePerson(foodData);
            mainApp.calculate();
        }
    }

    public function washingMachineChanged(calc:Boolean):void {
        var foodData:FoodSelectedData = createFoodSelectedData();
        mainApp.widok_sceny.Kuch.Pralka();
        if (calc) {
            updatePerson(foodData);
            mainApp.calculate();
        }
    }

    public function dishWasherChanged(calc:Boolean):void {
        var foodData:FoodSelectedData = createFoodSelectedData();
        mainApp.widok_sceny.Kuch.Zmywarka();
        if (calc) {
            updatePerson(foodData);
            mainApp.calculate();
        }
    }

    private function updatePerson(foodSelectedData:FoodSelectedData):void {
        mainApp.foodUpdate(foodSelectedData);

    }
}
}

